<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Core_Cookie {
	
	protected $_area = null;
	
	protected $_namespace = null;
	
	public function init($area, $namespace) {
		$this->_area = $area;
		$this->_namespace = $namespace;
	}
	
	public function set($name, $value, $path = null) {
		if (is_null($path))
			$path = $this->getBasePath();

		if ($this->_area && $this->_namespace)
			$name = $this->_area . '_' . $this->_namespace . '_' . $name;

		$expire = time() + App::config()->get('cookie_lifetime');
		setcookie($name, $value, $expire, $path);
		return $this;
	}
	
	public function get($name = null) {
		if ($name==null)
			return$_COOKIE;
		else {
			if ($this->_area && $this->_namespace)
				$name = $this->_area . '_' . $this->_namespace . '_' . $name;
			return isset($_COOKIE[$name]) ? $_COOKIE[$name] : null;
		}
	}
	
	public function delete($name, $path = null) {
		if (is_null($path))
			$path = $this->getBasePath();

		if ($this->_area && $this->_namespace)
			$name = $this->_area . '_' . $this->_namespace . '_' . $name;

		setcookie($name, "", time()-3600, $path);
		return $this;
	}
	
	public function getBasePath() {
		return parse_url(App::url(), PHP_URL_PATH);
	}
}